#include "mainmenu.h"

mainmenu::mainmenu(){
	x_sprite = lnSpriteCreate(SCREEN_MAIN, SPRSIZE_32x32, COLMODE_16, selectArrowTiles, selectArrowPal);
	x_spriteAnim = lnSpriteAnimBind(x_sprite, selectArrowTiles, 1, 40);
	x_splash=0;
	x_menu=0;
	x_chooseCharacter=0;
	x_enterPassword=0;
	x_viewHighscores=0;
	currentOption=0;
	x_sprite->isHidden=true;
	cursorPosition.x=23;
	cursorPosition.y=43;
	player= Player::getInstance();
	player->setHidden(true);
	Game = game::getInstance();
}

mainmenu::~mainmenu(){
	//delete x_sprite;
	//delete x_spriteAnim;
	x_sprite=NULL;
	x_spriteAnim=NULL;
}

void mainmenu::updateInput(touchPosition *touch){
	scanKeys();
	*touch = touchReadXY();
}

void mainmenu::displaySplash(){
	x_splash=1;
	dmaCopyHalfWords(DmaChannel, splashScreenBitmap, (uint16 *)BG_BMP_RAM(8),splashScreenBitmapLen);
	while(x_splash){
		updateInput(&touch);
		if(keysDown() & KEY_START){
			x_splash = 0;
		}
	}
	displayMenu();
}

void mainmenu::displayMenu(){
	cursorPosition.x=23;
	cursorPosition.y=43;
	currentOption=0;
	x_menu=1;
	dmaCopyHalfWords(DmaChannel, menuBitmap, (uint16 *)BG_BMP_RAM(8),menuBitmapLen);
	x_sprite->isHidden=false;
	while(x_menu){
		//consoleClear();
		//printf("option %i\n",currentOption);
		updateInput(&touch);
		handleInput();
		display();
		swiWaitForVBlank();
		lnSpritesUpdate();
	}
}

void mainmenu::handleInput(){
	if((keysDown() & KEY_TOUCH) || (keysHeld() & KEY_TOUCH)){
		t2DVector stylus;
		int stylusX = touch.px;
		int stylusY = touch.py;
		//printf("blah %i %i\n",(int)stylusX,(int)stylusY);
		//printf("blah");
		if(x_menu){
			if( ((stylusX > 75) && (stylusX < 175 )) || ((stylusY > 51) && (stylusY < 70)) ){
				
			}else if( ((stylusX > 50) && (stylusX < 205 )) || ((stylusY > 70) && (stylusY < 95)) ){
				currentOption=1;
				select();
			}else if( ((stylusX > 60) && (stylusX < 200 )) || ((stylusY > 95) && (stylusY < 117)) ){
				currentOption=1;
				select();
			}
		}else if(x_enterPassword){
			if( ((stylusX > 43) && (stylusX < 68 )) || ((stylusY > 39) && (stylusY < 63)) ){
				currentOption=0;
				select();
			}else if( ((stylusX > 109) && (stylusX < 134 )) || ((stylusY > 39) && (stylusY < 63)) ){
				currentOption=1;
				select();
			}else if( ((stylusX > 175) && (stylusX < 200 )) || ((stylusY > 39) && (stylusY < 63)) ){
				currentOption=2;
				select();
			}else if( ((stylusX > 43) && (stylusX < 68 )) || ((stylusY > 82) && (stylusY < 106)) ){
				currentOption=3;
				select();
			}else if( ((stylusX > 175) && (stylusX < 200 )) || ((stylusY > 39) && (stylusY < 63)) ){}else if( ((stylusX > 109) && (stylusX < 134 )) || ((stylusY > 82) && (stylusY < 106)) ){
				currentOption=4;
				select();
			}else if( ((stylusX > 175) && (stylusX < 200 )) || ((stylusY > 82) && (stylusY < 106)) ){
				currentOption=5;
				select();
			}else if( ((stylusX > 43) && (stylusX < 68 )) || ((stylusY > 128) && (stylusY < 152)) ){
				currentOption=6;
				select();
			}else if( ((stylusX > 109) && (stylusX < 134 )) || ((stylusY > 128) && (stylusY < 152)) ){
				currentOption=7;
				select();
			}else if( ((stylusX > 175) && (stylusX < 200 )) || ((stylusY > 128) && (stylusY < 152)) ){
				currentOption=8;
				select();
			}
		}
	}


	if(keysDown() & KEY_A){
		select();
	}
	
	if(keysDown() & KEY_B){
		goBack();
	}

	if(keysDown() & KEY_START){
		done();
	}

	if(keysDown() & KEY_UP){
		moveCursorUp();
	}
	if(keysDown() & KEY_DOWN){
		moveCursorDown();
	}
	if(keysDown() & KEY_RIGHT){
		moveCursorRight();
	}
	if(keysDown() & KEY_LEFT){
		moveCursorLeft();
	}
}

void mainmenu::update(){
	handleAnimation();
	display();
}

void mainmenu::display(){
	consoleClear();
	//printf("option %i\n",currentOption);
	x_sprite->posX = (int)cursorPosition.x;
	x_sprite->posY = (int)cursorPosition.y;
	//handleAnimation();
}

void mainmenu::printPassword(){
	//consoleClear();
	printf ("%s",password.c_str());
}

void mainmenu::select(){
	if(x_menu){
		x_menu=0;
		if(currentOption == 0){
			startGame();
		}else if(currentOption == 1){
			chooseCharacter();
		}else if(currentOption == 2){
			enterPassword();
		}else if(currentOption == 3){
			viewHighscores();
		}
	}else if(x_chooseCharacter){
		x_chooseCharacter=0;
	}else if(x_enterPassword){
		char number=currentOption+49;
		password.push_back(number);
	}
}

void mainmenu::goBack(){
	if((!x_menu) && (!x_enterPassword)){
		x_menu=1;
		displayMenu();
	}else if(x_enterPassword){
		if(password.empty()){
			x_enterPassword = 0;
			goBack();
		}else{
			std::string::iterator passwordIter;
			passwordIter = password.end()-1;
			password.erase(passwordIter);
		}
	}
}

void mainmenu::moveCursorUp(){
	if(x_menu){
		if(currentOption > 0){
			cursorPosition.y -= 23;
			currentOption--;
		}
	}else if(x_enterPassword){
		if(currentOption > 2){
			cursorPosition.y -= 46;
			currentOption -= 3;
		}
	}
}

void mainmenu::moveCursorDown(){
	if(x_menu){
		if(currentOption < 2){
			cursorPosition.y += 23;
			currentOption++;
		}
	}else if(x_enterPassword){
		if(currentOption < 6){
			cursorPosition.y += 46;
			currentOption += 3;
		}
	}
}

void mainmenu::moveCursorRight(){
	if(x_menu){
		//Do nothing here.
	}else if(x_enterPassword){
		if(currentOption%3 <= 1){
			cursorPosition.x += 65;
			currentOption++;
		}
	}
}

void mainmenu::moveCursorLeft(){
	if(x_menu){
		//Do nothing here.
	}else if(x_enterPassword){
		if(currentOption%3 >= 1){
			cursorPosition.x -= 65;
			currentOption--;
		}
	}
}

void mainmenu::done(){
	//Sean, we left the 111, 222, etc passwords there for you to use debugging. Our actual passwords are the numerical 
	//ASCII representations of ANDREW, DREA, KYLER, and DECISIVE, respectively
	if(x_enterPassword){
		if(password == "111" || password == "657868826987"){
			lnSpriteDestroy(x_sprite);
			player->setHidden(false);
			Game->startLevel(1);
			x_sprite = NULL;
			x_spriteAnim = NULL;
			while(1){
				updateInput(&touch);
				Game->update();
				swiWaitForVBlank();
				lnSpritesUpdate();
			}
		}else if(password == "222" || password == "68826965"){
			lnSpriteDestroy(x_sprite);
			player->setHidden(false);
			Game->startLevel(2);
			x_sprite = NULL;
			x_spriteAnim = NULL;
			while(1){
				updateInput(&touch);
				Game->update();
				swiWaitForVBlank();
				lnSpritesUpdate();
			}
			//password = "start level 2";
		}else if(password == "333" || password == "7589766982"){
			lnSpriteDestroy(x_sprite);
			player->setHidden(false);
			Game->startLevel(3);
			x_sprite = NULL;
			x_spriteAnim = NULL;
			while(1){
				updateInput(&touch);
				Game->update();
				swiWaitForVBlank();
				lnSpritesUpdate();
			}
		}else if(password == "444" || password == "6869677383738669"){
			lnSpriteDestroy(x_sprite);
			player->setHidden(false);
			Game->startLevel(4);
			x_sprite = NULL;
			x_spriteAnim = NULL;
			while(1){
				updateInput(&touch);
				Game->update();
				swiWaitForVBlank();
				lnSpritesUpdate();
			}
			password = "start level 4";
		}else{
			enterPassword();
		}
	}
}

void mainmenu::startGame(){
	lnSpriteDestroy(x_sprite);
	player->setHidden(false);
	Game->startGame();
	//delete x_sprite;
	x_sprite = NULL;
	x_spriteAnim = NULL;
	while(1){
		updateInput(&touch);
		Game->update();
		swiWaitForVBlank();
		lnSpritesUpdate();
	}
}

void mainmenu::chooseCharacter(){
}

void mainmenu::enterPassword(){
	password="";
	currentOption=0;
	cursorPosition.x=19;
	cursorPosition.y=34;
	x_enterPassword = 1;
	dmaCopyHalfWords(DmaChannel, passwordSelectBitmap, (uint16 *)BG_BMP_RAM(8),passwordSelectBitmapLen);
	while(x_enterPassword){
		updateInput(&touch);
		handleInput();
		display();
		printPassword();
		swiWaitForVBlank();
		lnSpritesUpdate();
	}
}

void mainmenu::viewHighscores(){
}

void mainmenu::handleAnimation(){
	x_spriteAnim->animStart = 0;
	x_spriteAnim->animEnd = 1;
	lnSpriteAnimLoop(x_sprite, ANIMTYPE_LOOP,0,1);
}